<template>
  <div class="page-common-layout oidc-callback-container">
    <a-spin size="large" tip="权限认证中..." :spinning="loading"></a-spin>
  </div>
</template>
<script>
import Vue from "vue";
import { axios } from "@/utils/request";
import { ACCESS_TOKEN, BLACK_TOKEN } from "@/store/mutation-types";
import { setCookie } from "@/utils/TopVUI.js";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      loading: true,
    };
  },
  mounted() {
    let { code, codeVerifier } = this.$route.query;
    if (code && codeVerifier) {
      this.oaLogin();
    } else {
      this.loginError();
    }
  },
  methods: {
    ...mapActions(["Logout"]),
    oaLogin() {
      this.loading = true;
      let { code, codeVerifier } = this.$route.query;
      return new Promise((resolve, reject) => {
        axios
          .post(
            "/system/shjkOaLogin",
            {
              codeVerifier,
              code,
            },
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          )
          .then((res) => {
            let { statusCode, blackToken, message } = res;
            if (statusCode == 200) {
              let token = message.substring(7);
              this.$store.commit("SET_TOKEN", token);
              Vue.ls.set(ACCESS_TOKEN, token);
              setCookie("token", token);
              Vue.ls.set(BLACK_TOKEN, blackToken);
              setCookie("blackToken", blackToken);
              setTimeout((_) => {
                this.loginSuccess(res);
              }, 500);
            } else {
              this.loginError();
              this.$message.error("身份认证失败，请重新登录~");
            }
            resolve();
          })
          .finally((_) => {
            this.loading = false;
            reject();
          });
      });
    },

    //登陆成功
    loginSuccess(res) {
      this.getLanguageMap();
      let { firstLogin } = res;
      switch (Number(firstLogin)) {
        //首次登录进入修改密码页面
        case 0:
          this.$router.push({
            path: "/firstPassword",
          });
          break;
        //进入系统
        default:
          this.$router.push({
            path: "/excessive",
          });
          break;
      }
    },

    //登陆失败
    loginError() {
      localStorage.clear();
      this.$router.push({
        path: "/user/login",
      });
    },

    // 获取多语言数据
    getLanguageMap() {
      this.$post("/system/sysLanguage/getLanguageMap").then((langRes) => {
        if (langRes.statusCode === 200) {
          let langData = langRes.data;
          this.$post("/system/sysLanguageConfig/selectAll").then((typeRes) => {
            let langTypeData = [];
            if (typeRes.statusCode === 200) {
              langTypeData = typeRes.data.map((e) => e.configValue);
            }
            let langTypeMap = {};
            for (let i = 0; i < langTypeData.length; i++) {
              let langDataMap = {};
              for (let key in langData) {
                if (key.indexOf("_" + langTypeData[i]) >= 0) {
                  langDataMap[key.replace("_" + langTypeData[i], "")] =
                    langData[key];
                  langTypeMap[langTypeData[i]] = langDataMap;
                }
              }
              this.$i18n.setLocaleMessage(
                langTypeData[i],
                langTypeMap[langTypeData[i]]
              );
            }
          });
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.oidc-callback-container {
  position: relative;
  /deep/.ant-spin {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .ant-spin-text {
      margin-top: 10px;
    }
  }
}
</style>
